package draw;

import game.GameField;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class DrawPanel extends JPanel {

	GameField field;
	private ZombieKeyListener zListener;

	public DrawPanel(GameField field) {
		zListener = new ZombieKeyListener();
		addKeyListener(zListener);
		addMouseListener(zListener);
		addMouseMotionListener(zListener);
		this.setFocusable(true);
		this.field = field;
		this.setPreferredSize(new Dimension(field.sx * field.bsize, field.sy
				* field.bsize));
		Timer timer = new Timer(10, new TimeListener());
		timer.start();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		field.draw(g);
	}

	private class TimeListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			field.run();
			repaint();
		}

	}

	private class ZombieKeyListener implements KeyListener,MouseListener,MouseMotionListener {

		@Override
		public void keyPressed(KeyEvent e) {
			field.k[e.getKeyCode()] = true;
			if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
				System.exit(0);
			
			

		}

		@Override
		public void keyReleased(KeyEvent e) {
			field.k[e.getKeyCode()] = false;
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			
			field.shooting = true;
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			field.shooting = false;
			
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			field.mousex = e.getX();
			field.mousey = e.getY();
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			field.mousex = e.getX();
			field.mousey = e.getY();
		}
	}

}
